#include <stdio.h>

#include <global/types.h>
#include <global/const.h>

#include <logger/logger.h>

static inline void usage()
{
	fprintf(stdout, "Usage:\n");
	fprintf(stdout, "None: ./test_logger ${logname} None\n");
	fprintf(stdout, "CutByTime: ./test_logger ${logname} CutByTime ${timeblock}\n");
	fprintf(stdout, "CutBySize: ./test_logger ${logname} CutBySize ${sizeblock}\n");
	fprintf(stdout, "RollByTime: ./test_logger ${logname} RollByTime ${timeblock} ${maxnum}\n");
	fprintf(stdout, "RollBySize: ./test_logger ${logname} RollBySize ${sizeblock} ${maxnum}\n");
}

static inline t_log * create_log(int argc, char ** argv)
{
	if (argc >= 3)
	{
		if (argc == 3 && strcmp("None", argv[2]) == 0)
		{
			return log_create(argv[1]);
		}
		else if (argc == 4)
		{
			if (strcmp("CutByTime", argv[2]) == 0)
			{
				return log_create_cbt(argv[1], atoi(argv[3]));
			}
			else if (strcmp("CutBySize", argv[2]) == 0)
			{
				return log_create_cbs(argv[1], atoi(argv[3]));
			}
		}
		else if (argc == 5)
		{
			if (strcmp("RollByTime", argv[2]) == 0)
			{
				return log_create_rbt(argv[1], atoi(argv[3]), atoi(argv[4]));
			}
			else if (strcmp("RollBySize", argv[2]) == 0)
			{
				return log_create_rbs(argv[1], atoi(argv[3]), atoi(argv[4]));
			}
		}
	}
	return NULL;
}

int main(int argc, char ** argv)
{
	const char * ss[] = {
		"Test Message 0",
		"Test Message 1",
		"Test Message 2",
		"Test Message 3",
		"Test Message 4"
	};

	t_log * log;

	log = create_log(argc, argv);

	if (log == NULL)
	{
		usage();
		return -1;
	}

	log_level(log, LOG_INFO);

	for (;;)
	{
		log_write(log, "%s", ss[0]);
		sleep(5);
	}

	return 0;
}
